#include<bits/stdc++.h>
using namespace std;
int mx[1005][1005];
int vis[1005][1005];
int n,m;
int dx[] = {1,-1,0};
int dy[] = {1,-1,0};
int check(int x,int y){
	int flag = 0;
	//九个格子都为0，才是空格
	for(int i = 0;i < 3;i++){
		for(int j = 0;j < 3;j++){
			int nx = x + dx[i];
			int ny = y + dy[j];
			if(nx <= 0 || ny <= 0 || nx > n || ny > m){
				continue;
			}
			if(mx[nx][ny] != 0) flag = 1;
		}
	}
	if(flag) return 0;
	else return 1;
}
int dfs(int x,int y){
	if(x <= 0 || y <= 0 || x > n || y > m || vis[x][y] == 0){
		return 0;
	}
	vis[x][y] = 0;
	return 1 + dfs(x - 1,y) + dfs(x + 1,y) + dfs(x,y + 1) + dfs(x,y - 1) + 
	dfs(x + 1,y + 1) + dfs(x - 1,y + 1) + dfs(x + 1,y - 1) + dfs(x - 1,y - 1) ;
}
int main(){

	cin >> n >> m;
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= m;j++){
			cin >> mx[i][j];
		}
	}
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= m;j++){
			vis[i][j] = check(i,j);
		}
	}
	int cnt = 0;
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= m;j++){
			if(dfs(i,j) > 0){
				cnt++;
			}
		}
	}

	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= m;j++){
			if(mx[i][j]) continue;
			int flag = 0;
			//(i,j)上下左右都不是空格，且存在雷
			for(int k = 0;k < 3;k++){
				for(int l = 0;l < 3;l++){
					int nx = i + dx[k];
					int ny = j + dy[l];
					if(nx <= 0 || ny <= 0 || nx > n || ny > m ){
						continue;
					}
					if(check(nx,ny)) flag = 1;//说明有空格
				}
			}
			//有空格不行
			if(flag){
				flag = 0;
				continue;
			}
			flag = 0;
			//没空格的时候就找雷
			for(int k = 0;k < 3;k++){
				for(int l = 0;l < 3;l++){
					int nx = i + dx[k];
					int ny = j + dy[l];
					if(nx <= 0 || ny <= 0 || nx > n || ny > m ){
						continue;
					}
					if(mx[nx][ny] == 1) flag = 1;//说明有空格
				}
			}
			if(flag){
				flag = 0;
				cnt++;
			}
		}
	}
	cout << cnt << "\n";
}